<template>
	<div class="shopcart-container">
		<!-- 全选 清空 -->
		<div class="cart_header">
			<div class="cart_title">
				<!-- 全选 -->
				<van-checkbox checked-color="#000000" icon-size="18" v-model="isCheckedAll" @click="onSelectAll">全选
				</van-checkbox>
			</div>
			<div class="cart_clear" @click="onClickDeleteAll">
				<van-icon name="delete" />
				<span>清空</span>
			</div>
		</div>
		<!-- 商品列表区域 -->
		<div style="background-color: white;">
			<van-row v-for="(item, i) in cartList" :key="i" type="flex" align="center">
				<van-col span="2">
					<!-- 复选框 -->
					<van-checkbox v-model="item.is_checked" @change="onCheckedChange(item)" />
				</van-col>
				<van-col span="5">
					<!-- 中间商品图片 -->
					<van-icon size="80" :name="APIDomainName + item.pic"></van-icon>
				</van-col>
				<van-col span="17">
					<van-row class="van-multi-ellipsis--l2 title">{{ item.title }}</van-row>
					<van-row  type="flex" align="center">
						<van-col span="8" class="price">{{ item.price }}</van-col>
						<van-col span="16" class="opration">
							<van-stepper v-model="item.count" @change="onCountChanged(item)" integer />
							<van-button text="删除" size="small" @click.prevent="onClickDeleteCartItem(item.id)" />
						</van-col>
					</van-row>
				</van-col>
			</van-row>
		</div>
		<!-- 商品结算区域 -->
		<van-cell title="商品结算" value="总计不含运费"></van-cell>
		<van-cell>
			<template #title>
				已勾选商品<span class="red">{{ getCheckedCount }}</span>件，总价<span class="red">¥{{ getCheckedAmount }}</span>
			</template>
		</van-cell>
		<!-- 结算按钮 -->
		<van-button type="primary" size="large" @click="onClickCreateOrder">去结算</van-button>
	</div>
</template>

<script>
	import {
		getCartListAPI,
		updateCartCountAPI,
		updateCartCheckedAPI,
		deleteCartItemAPI,
		deleteCartAllAPI, //删除清空购物车接口
		updateCartCheckedAllAPI, //全选/全不选购物车项接口
	} from "@/api/cart.js";
	import { showConfirmDialog } from 'vant';
	export default {
		data() {
			return {
				cartList: [], //购物车列表数据
				isCheckedAll: false, //是否全选
			};
		},
		//生命周期方法
		created() {
			this.getCartData();
		},
		//计算属性
		computed: {
			getCheckedCount() { //计算勾选记录总数
				let sum = 0;
				this.cartList.forEach((item) => {
					if (item.is_checked) sum += 1;
				});
				return sum;
			},
			getCheckedAll() { //计算是否全部勾选
				return this.cartList.length == this.getCheckedCount ? true : false;
			},

			getCheckedCount() { //计算勾选记录总数
				let sum = 0;
				this.cartList.forEach((item) => {
					if (item.is_checked) sum += 1;
				});
				return sum;
			},

			getCheckedAmount() { //计算勾选商品总金额
				let amount = 0;
				this.cartList.forEach((item) => {
					if (item.is_checked) amount += item.price * item.count;
				});
				return amount;
			},
		},

		//方法定义
		methods: {
			//获取用户购物车数据
			getCartData() {
				this.cartList = []; //清空
				getCartListAPI().then(result => {
					this.cartList = result.data;
					this.isCheckedAll = this.getCheckedAll; // 重置isCheckedAll属性值
				}).catch(() => {});
			},

			//触发更改购物商品数量
			onCountChanged(item) {
				//准备参数
				let params = {
					id: item.id,
					count: item.count
				};
				//调用接口
				updateCartCountAPI(params).then(() => {});
			},

			//触发更改购物商品选中状态
			onCheckedChange(item) {
				let id = item.id;
				let is_checked = item.is_checked == true ? 1 : 0;
				let params = { id, is_checked };
				updateCartCheckedAPI(params).then(() => {
					this.isCheckedAll = this.getCheckedAll; // 重置isCheckedAll属性值
				}).catch(() => {});
			},

			//触发删除购物商品
			onClickDeleteCartItem(id) {
				let ids = id;
				deleteCartItemAPI(ids).then(() => {
					this.getCartData();
				});
			},
			//触发全选/全不选
			onSelectAll() {
				let is_checked = this.isCheckedAll == true ? 1 : 0;
				updateCartCheckedAllAPI(is_checked).then(() => {
					this.getCartData();
				});
			},
			//触发清空
			onClickDeleteAll() {
				showConfirmDialog({
					message: '客官，您确定清空购物车吗?'
				}).then(() => {
					deleteCartAllAPI().then(() => {
						this.getCartData();
					});
				})
			},

			//触发创建订单
			onClickCreateOrder() {
				if (this.cartList.length === 0) {
					showToast("您的购物车为空");
					return;
				}
				//跳转到创建订单页面（根据购物车创建）
				this.$router.push({
					name: "orderCreate"
				});
			},

		},
	};
</script>

<style lang="less" scoped>
.shopcart-container {
  padding: 0 12px;
  background: #f8f9fa;
  min-height: 100vh;

  .cart_header {
    padding: 16px;
    margin: 12px 0;
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    display: flex;
    align-items: center;
    justify-content: space-between;
    
    .cart_clear {
      color: #e74c3c;
      display: flex;
      align-items: center;
      gap: 4px;
      transition: all 0.3s;
      
      &:active {
        transform: scale(0.95);
      }
    }
  }

  .van-row {
    margin: 8px 0;
    padding: 12px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    transition: all 0.3s;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
  }
}

.van-image {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
}

.title {
  font-size: 14px;
  font-weight: 500;
  color: #2c3e50;
  line-height: 1.4;
  margin-bottom: 8px;
}

.price {
  color: #e74c3c !important;
  font-size: 16px;
  font-weight: 800;
  
  &::before {
    content: "¥";
    font-size: 0.9em;
  }
}

.opration {
  gap: 8px;
  
  .van-button {
    border-radius: 15px;
    padding: 0 12px;
    background: linear-gradient(45deg, #ff4757, #e74c3c);
    border: none;
  }
}

.van-button--large {
  margin: 16px 0;
  border-radius: 25px;
  background: linear-gradient(45deg, #2c3e50, #34495e);
  font-weight: bold;
}
</style>